﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EYDIngenieria.ERP.Aplicacion.Mantenedores;
using EYDIngenieria.ERP.ObjetosDeNegocio.Dominio;

public partial class Mantenedores_MantenedorCliente : System.Web.UI.Page
{
    private EYDIngenieria.ERP.Aplicacion.Mantenedores.MantenedorCliente mantenedor = new MantenedorCliente();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session.Keys.Count == 0)
            Response.Redirect("../Default.aspx");
        if(!IsPostBack)
            MantenedorVistaInicial();
               
    }
    
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SetearFormulario();
        MantenedorVisualizacion();
    }

    private void SetearFormulario()
    {
        int id = (gdvCliente.SelectedValue == null) ? 0 : int.Parse(gdvCliente.SelectedValue.ToString());
        txbRazonSocial.Text = string.Empty;
        txbRut.Text = string.Empty;
        txbDireccion.Text = string.Empty;
        txbTelefono.Text = string.Empty;
        txbMail.Text = string.Empty;
        txbContacto.Text = string.Empty;
        txbGiro.Text = string.Empty;
        if(id>0)
        {
            Cliente u = mantenedor.Obtener(id);
            txbRazonSocial.Text = u.RazonSocial;
            txbRut.Text = u.Rut;
            txbDireccion.Text = u.Direccion;
            txbTelefono.Text = u.Telefono;
            txbMail.Text = u.Mail;
            txbContacto.Text = u.Contacto;
            txbGiro.Text = u.Giro;
        }
        
    }

    private void MantenedorVistaInicial()
    {
        txbRut.Enabled = false;
        txbRazonSocial.Enabled = false;
        txbDireccion.Enabled = false;
        txbTelefono.Enabled = false;
        txbMail.Enabled = false;
        txbContacto.Enabled = false;
        txbGiro.Enabled = false;
        btnNuevo.Visible = true;
        btnAceptar.Visible = false;
        btnCancelar.Visible = false;
        btnEliminar.Visible = false;
        btnModificar.Visible = false;
        SetearFormulario();
    }

    private void MantenedorVisualizacion()
    {
        SetearFormulario();
        txbRut.Enabled = false;
        txbRazonSocial.Enabled = false;
        txbDireccion.Enabled = false;
        txbTelefono.Enabled = false;
        txbMail.Enabled = false;
        txbContacto.Enabled = false;
        txbGiro.Enabled = false;
        btnNuevo.Visible = true;
        btnAceptar.Visible = false;
        btnCancelar.Visible = false;
        btnEliminar.Visible = true;
        btnModificar.Visible = true;
        
    }

    private void MantenedorVistaGestion()
    {
        SetearFormulario();
        txbRut.Enabled = true;
        txbRazonSocial.Enabled = true;
        txbDireccion.Enabled = true;
        txbTelefono.Enabled = true;
        txbMail.Enabled = true;
        txbContacto.Enabled = true;
        txbGiro.Enabled = true;
        btnNuevo.Visible = false;
        btnAceptar.Visible = true;
        btnCancelar.Visible = true;
        btnEliminar.Visible = false;
        btnModificar.Visible = false;
    }

    protected void btnNuevo_Click(object sender, EventArgs e)
    {
        gdvCliente.SelectedIndex = -1;
        SetearFormulario();
        MantenedorVistaGestion();
    }
    protected void btnModificar_Click(object sender, EventArgs e)
    {      
        MantenedorVistaGestion();
    }
    protected void btnEliminar_Click(object sender, EventArgs e)
    {
        int id = (gdvCliente.SelectedValue == null) ? 0 : int.Parse(gdvCliente.SelectedValue.ToString());
        if(id ==0) 
            return;

        Cliente u = mantenedor.Obtener(id);
        u.Rut = txbRut.Text;
        u.RazonSocial = txbRazonSocial.Text;
        u.Direccion = txbDireccion.Text;
        u.Telefono = txbTelefono.Text;
        u.Mail = txbMail.Text;
        u.Contacto = txbContacto.Text;
        u.Giro = txbGiro.Text;
        mantenedor.Eliminar(u);
        gdvCliente.SelectedIndex = -1;
        ActualizaDataSource();
        SetearFormulario();
    }
    protected void btnCancelar_Click(object sender, EventArgs e)
    {
        int id = (gdvCliente.SelectedValue == null) ? 0 : int.Parse(gdvCliente.SelectedValue.ToString());
        if(id>0)
        MantenedorVisualizacion();
        else
            MantenedorVistaInicial();
    }
    protected void btnAceptar_Click(object sender, EventArgs e)
    {
        int id = (gdvCliente.SelectedValue == null) ? 0 : int.Parse(gdvCliente.SelectedValue.ToString());
        EYDIngenieria.ERP.ObjetosDeNegocio.Dominio.Cliente o = new Cliente();
        o.Rut = txbRut.Text;
        o.RazonSocial = txbRazonSocial.Text;
        o.Id = id;
        o.Direccion = txbDireccion.Text;
        o.Telefono = txbTelefono.Text;
        o.Mail = txbMail.Text;
        o.Contacto = txbContacto.Text;
        o.Giro = txbGiro.Text;
        o.Activo = true;
        if(id==0) 
            mantenedor.Agregar(o);
        else
            mantenedor.Modificar(o);
       
        ActualizaDataSource();
        MantenedorVistaInicial();
       
    }

    private void ActualizaDataSource()
    {
        odsCliente.Select();
        odsCliente.DataBind();
        gdvCliente.DataBind();
        
       
    }
    
}
